#!/bin/sh
# Convert DevHelp bookname into a CHM file.
# Made for Cygwin, as CyGNOME DevHelp program runs too slowly
# But you can use it to generate .hhp, .hhc and .hhk file in other system.
# 
# Some assumption:
#     The book name and the directory name are the same, 
#     i.e, all html files of foo.devhelp are in directory /some/where/foo/.
#
#     If option -d is not specified, I could think the foo.devhelp file
#     and book directory foo/ are in the same dir.
#
#     File pathes in .hhk, .hhc and .hhk are relative path to the parent of 
#     the directory where html files are kept 
#     (i.e, reletive to /usr/share/devhelp/books in most cases).
#
# Requirements:
#     xsltproc (in package libxslt2), to translate .devhelp file
#     M$ HTMLHelp Workshop (or hhc.exe only), to compile CHM project
#
# TO-DO:
#     Multi-book support (into one project)
#
# Changes"
#     Mar 17, 2004
#       Added option: --copy-files, --auto-build, --start-workshop
#	Embeded devhelp2hhc.xsl and devhelp2hhc.xsl
#     Mar 16, 2004 
#       First version
#
#Author
#     Ralgh Young (ralgh_young@yahoo.com)



Usage() 
{
    echo "Usage: $0 [OPTIONS] foo.devhelp"
    echo "     convert DevHelp book into a CHM file"
    echo "     -d dir               where html files are kept (if not kept with .devhelp file"
    echo "     -c|--copy-files      copy whole book to current directory    " 
    echo "     -b|--auto-build      start build when project file generated (hhc.exe needed)"
    echo "     -w|--start-workshop  use HTMLHelp workshop to open project file when done"
    echo "     e.g $0 -d /usr/share/devhelp/books/gdkpixbuf /usr/share/devhelp/specs/gdkpixbuf.devhelp"
}

if [ $# -eq 0 ]; then
    Usage
    exit 1
fi

if [ x$1 = 'x-h' -o x$1 = '--help' ]; then
    Usage
    exit 0
fi

# e.g
# ./devhelp2chm.sh -d /usr/share/devhelp/books/gdkpixbuf /usr/share/devhelp/specs/gdkpixbuf.devhelp
#  filesdir = /usr/share/devhelp/books/gdkpixbuf    
#  bookpath=gdkpixbuf   # last part of $filesdir
#  bookname=gdkpixbuf   # basename of gdkpixbuf.devhelp
#  
verbose=0
auto_build=0
start_workshop=0
filesdir=""
while [ $# -gt 0 ]; do
    case $1 in
     -v|--verbose)  verbose=1; shift;;
     -c|--copy)     copy=1;  shift;;
     -d|--dir)      filesdir=$2; shift 2;;
     -b|--auto-build)    auto_build=1; shift;;
     -w|--start-workshop)     start_workshop=1; shift;;  
     --)  shift;  break;;
     -*) echo " '$1': unknown option"; exit 1;;
     [^-]*)  break;;
    esac
done

filename="$1"
bookname=`basename $filename .devhelp`
if [ x$filesdir != "x" ]; then
    filename="$1"
    bookpath=`basename $filesdir`
    bookname=`basename $filename .devhelp`
else
    filesdir=`dirname $filename`/${bookname}
fi

    
if [ ! -f $filename ]; then
   echo "File not exists: $filename "
   exit 2
fi   

# ============ Embedded devhelp2hhc.xsl =========================
# Note: the variable $bookname would be replaced by shell
cat >devhelp2hhc.xsl << EOF_DEVHELP2CHM_XSL
<xsl:stylesheet version='1.0'
		xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
		xmlns='http://www.w3.org/TR/REC-html40'>
 <!-- autogenerated by devhelp2chm.sh
      don't modify it manually, modify devhelp2chm.sh instead -->

 <xsl:output method="html" version="3.2" omit-xml-declaration="yes"/>
 
 <xsl:template match="/">
    <HTML>
    <HEAD>
       <TITLE><xsl:value-of select="/book/@title"/></TITLE>
    <!-- Sitemap 1.0 -->
    </HEAD>
    <BODY>
      <UL>
<!-- If you want the root node, comment the lines followed -->
         <LI>
          <OBJECT type="text/sitemap">
   <!--	  <param name="Name" value="The GdkPixbuf Structure ">
	  <param name="Local" value="gdkpixbuf\gdk-pixbuf-gdk-pixbuf.html"> -->     
	  <xsl:text disable-output-escaping="yes">
	  &lt;param name="Name" value="</xsl:text>
	  <xsl:value-of select="/book/@title"/>
	  <xsl:text disable-output-escaping="yes">"&gt;</xsl:text>
	  
	  <xsl:text disable-output-escaping="yes">	  
	  &lt;param name="Local" value="</xsl:text>$bookpath/<xsl:value-of select="/book/@link"/>          
	  <xsl:text disable-output-escaping="yes">"&gt;
	  </xsl:text>
	  </OBJECT>	  
        </LI>
        <UL>
<!-- root node: ends -->

        <xsl:apply-templates select="/book/chapters"/>
<!-- root node: begin -->          
        </UL>
<!-- root node: end -->        
       </UL>          
    </BODY>
    </HTML>
 </xsl:template>

 <xsl:template match="chapters">
      <xsl:for-each select="sub"> 
          <xsl:apply-templates select="."/>
      </xsl:for-each>
 </xsl:template>

 <xsl:template match="sub">
      <LI>
          <OBJECT type="text/sitemap">
<!--	  <param name="Name" value="The GdkPixbuf Structure ">
	  <param name="Local" value="gdkpixbuf\gdk-pixbuf-gdk-pixbuf.html"> -->     
	  <xsl:text disable-output-escaping="yes">
	  &lt;param name="Name" value="</xsl:text>
	  <xsl:value-of select="@name"/>
	  <xsl:text disable-output-escaping="yes">"&gt;</xsl:text>
	  
	  <xsl:text disable-output-escaping="yes">
	  &lt;param name="Local" value="</xsl:text>$bookpath/<xsl:value-of select="@link"/>          
	  <xsl:text disable-output-escaping="yes">"&gt;
	  </xsl:text>
	  </OBJECT>	  
      </LI>
      <xsl:if test="count(sub)>0">
          <UL>
          	<xsl:apply-templates select="sub"/>	
          </UL>
      </xsl:if>   
 </xsl:template>

</xsl:stylesheet>
EOF_DEVHELP2CHM_XSL

# ================== Embed devhelp2hhk.xsl  ==============
# Note: the variable $bookname would be replaced by shell
cat > devhelp2hhk.xsl << EOF_DEVHELP2HHK_XSL
<xsl:stylesheet version='1.0'
		xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
		xmlns='http://www.w3.org/TR/REC-html40'>
 <!-- autogenerated by devhelp2chm.sh
      don't modify it manually, modify devhelp2chm.sh instead -->
 <xsl:output method="html" version="3.2" omit-xml-declaration="yes"/>

 <xsl:template match="/">
    <HTML>
    <HEAD>
       <TITLE><xsl:value-of select="/book/@title"/></TITLE>
    <!-- Sitemap 1.0 -->
    </HEAD>
    <BODY>
      <UL>      
          <xsl:apply-templates select="/book/functions"/>
       </UL>          
    </BODY>
    </HTML>
 </xsl:template>

 <xsl:template match="functions">
      <xsl:for-each select="function"> 
          <xsl:apply-templates select="."/>
      </xsl:for-each>
 </xsl:template>

 <xsl:template match="function">
      <LI>
          <OBJECT type="text/sitemap">
<!--	  <param name="Name" value="g_tree_insert">
          <param name="Name" value="g_tree_insert">
	  <param name="Local" value="glib-balanced-binary-trees.html#G-TREE-INSERT"> -->     
	  <xsl:text disable-output-escaping="yes">
	  &lt;param name="Name" value="</xsl:text>
	  <xsl:value-of select="@name"/>
	  <xsl:text disable-output-escaping="yes">"&gt;</xsl:text>
	  
	  <xsl:text disable-output-escaping="yes">
	  &lt;param name="Name" value="</xsl:text>
	  <xsl:value-of select="@name"/>
	  <xsl:text disable-output-escaping="yes">"&gt;</xsl:text>

	  <xsl:text disable-output-escaping="yes">
	  &lt;param name="Local" value="</xsl:text>$bookpath/<xsl:value-of select="@link"/>          
	  <xsl:text disable-output-escaping="yes">"&gt;
	  </xsl:text>
	  </OBJECT>	  
      </LI>
 </xsl:template>

</xsl:stylesheet>
EOF_DEVHELP2HHK_XSL

# generate the content and index file
test $verbose -eq 1 && echo "Generating content file ${bookname}.hhc"
xsltproc devhelp2hhc.xsl $filename > ${bookname}.hhc
test $verbose -eq 1 && echo "Generating index file ${bookname}.hhk"
xsltproc devhelp2hhk.xsl $filename > ${bookname}.hhk

if [ $copy -eq 1 ];then
   test $verbose -eq 1 && echo "Copy whole book to ./${bookpath}"
   cp -Ru $filesdir $bookpath
fi
   
# now the project file
test $verbose -eq 1 && echo "Generating project file ${bookname}.hhp"

title=`grep "book title" $filename | cut -d"\"" -f2`
cat > ${bookname}.hhp <<EOF
[OPTIONS]
Compatibility=1.1 or later
Compiled file=${bookname}.chm
Contents file=${bookname}.hhc
Index file=${bookname}.hhk
Default topic=${bookname}/index.html
Display compile progress=No
Full-text search=Yes
Language=0x409
Title=$title

[FILES]
EOF

files=`cd $filesdir/..; find $bookpath -type f`
for f in $files; do
   echo $f >> ${bookname}.hhp
done   

# The following are Windows specific, you need cygstart in cygutils
if [ $auto_build -eq 1 ]; then
  test $verbose -eq 1 && echo -n "Start build ${bookname}.chm"
  cygstart hhc ${bookname}.hhp    
elif [ $start_workshop -eq 1 ]; then
  test $verbose -eq 1 && echo "Open project ${bookname}.hhp with HTMLHelp Workshop"
  cygstart hhw ${bookname}.hhp
  if [ $? -ne 0 ]; then
  	cygstart ${bookname}.hhp
  fi
fi
